/////////////////////////////////////////////////////////////////////////////
// Name:        mainframe.cpp
// Purpose:     
// Author:      Julian Smart
// Modified by: 
// Created:     22/03/2009 10:28:01
// RCS-ID:      
// Copyright:   
// Licence:     
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#include "ax/ax_containerctrl.h"
#include "ax/ax_controller_standard.h"
#include "ax/ax_view.h"
#include "ax/ax_menu_folder.h"
#include "ax/ax_menu_choice.h"
#include "ax/ax_menu_toggle.h"
#include "ax/ax_menu_text.h"
#include "ax/ax_menu_range.h"
#include "ax/ax_menu_speech_settings.h"
#include "ax/ax_menu_visual_settings.h"
#include "ax/ax_menu_shortcuts.h"
#include "ax/ax_messagebox.h"

#include "ax_resource/ax_resource.h"
#include "ax_resource/ax_activator_resource.h"
#include "ax_resource/ax_menu_library_settings.h"

////@begin includes
////@end includes

#include "mainframe.h"
#include "axsampleapp.h"
#include "samplesettingsdialog.h"

////@begin XPM images
#include "ax.xpm"
////@end XPM images

/*
 * SampleMainFrame type definition
 */

IMPLEMENT_CLASS( SampleMainFrame, wxFrame )


/*
 * SampleMainFrame event table definition
 */

BEGIN_EVENT_TABLE( SampleMainFrame, wxFrame )

////@begin SampleMainFrame event table entries
    EVT_CLOSE( SampleMainFrame::OnCloseWindow )

    EVT_MENU( ID_ADD_FROM_FOLDER, SampleMainFrame::OnAddFromFolderClick )

    EVT_MENU( wxID_PREFERENCES, SampleMainFrame::OnPreferencesClick )

    EVT_MENU( wxID_EXIT, SampleMainFrame::OnExitClick )

////@end SampleMainFrame event table entries

END_EVENT_TABLE()


/*
 * SampleMainFrame constructors
 */

SampleMainFrame::SampleMainFrame()
{
    Init();
}

SampleMainFrame::SampleMainFrame( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create( parent, id, caption, pos, size, style );
}


/*
 * SampleMainFrame creator
 */

bool SampleMainFrame::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin SampleMainFrame creation
    wxFrame::Create( parent, id, caption, pos, size, style );

    CreateControls();
    SetIcon(GetIconResource(wxT("ax.xpm")));
    Centre();
////@end SampleMainFrame creation
    return true;
}


/*
 * SampleMainFrame destructor
 */

SampleMainFrame::~SampleMainFrame()
{
////@begin SampleMainFrame destruction
////@end SampleMainFrame destruction
}


/*
 * Member initialisation
 */

void SampleMainFrame::Init()
{
////@begin SampleMainFrame member initialisation
    m_containerCtrl = NULL;
////@end SampleMainFrame member initialisation
}


/*
 * Control creation for SampleMainFrame
 */

void SampleMainFrame::CreateControls()
{    
////@begin SampleMainFrame content construction
    SampleMainFrame* itemFrame1 = this;

    wxMenuBar* menuBar = new wxMenuBar;
    wxMenu* itemMenu3 = new wxMenu;
    itemMenu3->Append(ID_ADD_FROM_FOLDER, _("&Add From Folder..."), _T(""), wxITEM_NORMAL);
    itemMenu3->Append(wxID_PREFERENCES, _("&Preferences...\tCtrl+T"), _T(""), wxITEM_NORMAL);
    itemMenu3->Append(ID_GO_UP, _("Go &Up"), _T(""), wxITEM_NORMAL);
    itemMenu3->AppendSeparator();
    itemMenu3->Append(wxID_EXIT, _("&Exit"), _T(""), wxITEM_NORMAL);
    menuBar->Append(itemMenu3, _("&File"));
    itemFrame1->SetMenuBar(menuBar);

    m_containerCtrl = new AxContainerCtrl( itemFrame1, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER|wxTAB_TRAVERSAL );

////@end SampleMainFrame content construction

    wxGetApp().GetViewManager().SetContainerCtrl(m_containerCtrl);
    wxGetApp().GetViewManager().SetViewPagerCtrl(m_containerCtrl->GetPagerCtrl());
    wxGetApp().GetViewManager().SetTitleCtrl(m_containerCtrl->GetTitleCtrl());
    wxGetApp().GetViewManager().SetDescriptionCtrl(m_containerCtrl->GetDescriptionCtrl());
    wxGetApp().GetViewManager().SetMenuCtrl(m_containerCtrl->GetMenuCtrl());

    wxString homeDescription = _("Welcome to the Accessibility Framework resource sample!\n\n\
Browse the resource library and use the built-in utilities, using the simple menu interface.\n\n\
The idea behind the framework is to provide a consistent UI for the user and reusable API for the developer.");

    wxGetApp().GetViewManager().SetHomeDescription(homeDescription);

    wxGetApp().GetViewManager().SetShortcutManager(& wxGetApp().GetShortcutManager());

    AxStandardMenuController* controller =
        new AxStandardMenuController(m_containerCtrl->GetMenuCtrl(), m_containerCtrl->GetTitleCtrl());

    wxGetApp().GetViewManager().SetMenuController(controller);
    controller->SetViewManager(& wxGetApp().GetViewManager());

    m_containerCtrl->SetController(controller);
    m_containerCtrl->GetMenuCtrl()->SetController(controller);

    m_adapter.Adapt(this);

    AxMenu* menu = new AxMenu(_("Main Menu"), _("This is the main menu."));
    AxMenuItem* menuItem = NULL;

#if 0    
    menuItem = new AxMenuItem(_("Library"), _("Your resource library"));
    menuItem->SetDynamic();
    wxArrayString fields, values;
    fields.Add(axPROPERTY_RESOURCE_TYPE);
    values.Add(wxT("book"));
    menuItem->SetActivator(new AxResourceLibraryActivator(menuItem, & wxGetApp().GetLibrary(), fields, values));
    menu->AddItem(menuItem);
#endif

    menuItem = new AxMenuItem(_("Library - all items"), _("All your resources"));
    menuItem->SetDynamic();
    menuItem->SetActivator(new AxResourceLibraryActivator(menuItem, & wxGetApp().GetLibrary(), AxResourceLibraryActivator::Search_All));
    menu->AddItem(menuItem);

    menuItem = new AxMenuItem(_("Settings"), _("Change program settings"));
    // menuItem->SetStyle(axMENU_STYLE_BOLD);
    menu->AddItem(menuItem);

    AxMenu* settingsMenu = new AxMenu(_("Settings"), _("Change program settings"));

    {
        // Library settings
        settingsMenu->AddItem(new AxResourceLibrarySettingsMenuItem(& wxGetApp().GetLibrary()));
        
        // Speech settings
        settingsMenu->AddItem(new AxSpeechSettingsMenuItem(controller, & wxGetApp().GetViewManager().GetSpeech()));
        
        // Visual settings
        settingsMenu->AddItem(new AxVisualSettingsMenuItem(controller, & wxGetApp().GetViewManager().GetSpeech()));

        // Shortcut settings
        settingsMenu->AddItem(new AxShortcutsMenuItem(controller,
            & wxGetApp().GetViewManager().GetSpeech(),
            & wxGetApp().GetShortcutManager(),
            this /* the window to set the shortcuts in */,
            GetMenuBar()));
    }

    menuItem->SetSubMenu(settingsMenu);
    settingsMenu->AddItem(new AxGoUpMenuItem);

    menu->AddItem(new AxResourceLibraryAddByFolderMenuItem(& wxGetApp().GetLibrary()));
    menu->AddItem(new AxResourceLibraryAddByFileMenuItem(& wxGetApp().GetLibrary()));

    menuItem = new AxMenuItem(_("Exit"), _("Quit the program"));
    menuItem->SetActivator(new AxEventActivator(menuItem, this, wxID_EXIT));
    menuItem->SetStyle(AxMenuStyleBold);
    menu->AddItem(menuItem);

    wxGetApp().GetViewManager().ShowInitialMenu(menu);
    wxGetApp().GetViewManager().ShowHomeView();
}

/*
 * Should we show tooltips?
 */

bool SampleMainFrame::ShowToolTips()
{
    return true;
}

/*
 * Get bitmap resources
 */

wxBitmap SampleMainFrame::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin SampleMainFrame bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end SampleMainFrame bitmap retrieval
}

/*
 * Get icon resources
 */

wxIcon SampleMainFrame::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin SampleMainFrame icon retrieval
    wxUnusedVar(name);
    if (name == _T("ax.xpm"))
    {
        wxIcon icon(ax_xpm);
        return icon;
    }
    return wxNullIcon;
////@end SampleMainFrame icon retrieval
}


/*
 * wxEVT_COMMAND_MENU_SELECTED event handler for wxID_EXIT
 */

void SampleMainFrame::OnExitClick( wxCommandEvent& WXUNUSED(event) )
{
    Close();
}


/*
 * wxEVT_COMMAND_MENU_SELECTED event handler for wxID_PREFERENCES
 */

void SampleMainFrame::OnPreferencesClick( wxCommandEvent& WXUNUSED(event) )
{
    SampleSettingsDialog dialog(this);
    dialog.ShowModal();
}


/*
 * wxEVT_COMMAND_MENU_SELECTED event handler for ID_ADD_FROM_FOLDER
 */

void SampleMainFrame::OnAddFromFolderClick( wxCommandEvent& WXUNUSED(event) )
{
    wxString msg = _("Choose the folder to search");
    wxWindow* parent = this;
    
    wxString originalPath;
    
    wxDirDialog dirDlg(parent, msg, originalPath, wxDD_DEFAULT_STYLE);
    if (dirDlg.ShowModal() == wxID_OK)
    {
        wxString path = dirDlg.GetPath();
        wxGetApp().GetLibrary().LoadFromFolder(path, false, true);
        wxGetApp().GetLibrary().RefreshAll();
        wxGetApp().GetLibrary().SaveLibrary();
    }
}


/*
 * wxEVT_CLOSE_WINDOW event handler for ID_SAMPLEMAINFRAME
 */

void SampleMainFrame::OnCloseWindow( wxCloseEvent& event )
{
    if (wxGetApp().GetViewManager().IsModal() && event.CanVeto())
    {
        AxMessageBox(_("Sorry, cannot quit because a menu choice needs to be made."));
        event.Veto();
        return;
    }
    else
    {
        if (IsMaximized())
            wxGetApp().m_frameStatus = SHOW_STATUS_MAXIMIZED ;
        else if (IsIconized())
            wxGetApp().m_frameStatus = SHOW_STATUS_MINIMIZED ;
        else
            wxGetApp().m_frameStatus = SHOW_STATUS_NORMAL ;
        
        if (!IsIconized() && !IsMaximized())
            wxGetApp().m_frameRect = GetRect();

        Show(false);
        wxGetApp().GetViewManager().ClearViews();
        Destroy();
    }
}
